<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>通用采集器系统</title>
    <link rel="stylesheet" href="css/login.css" />
    <link rel="stylesheet" href="../layui/css/layui.css" />
    <script src="../layui/layui.js"></script>
  </head>

  <body>
    <div
      class="content"
      style="margin-left: auto; margin-right: auto; width: 100%; height: 100%"
    >
      <section id="content">
        <div class="header">
          <h1 href="javascript:;" class="current">通用采集器系统登录</h1>
        </div>
        <div id="body">
          <form class="form">
            <div id="username">
              <h style="font-size: 18px;">账号</h>
              <input id="nameinput" type="text" placeholder="用户名" />
            </div>
            <div id="password">
              <h style="font-size: 18px;">密码</h>
              <input id="passwordinput" type="password" placeholder="密码" />
            </div>
            <div id="s1">
              <input type="checkbox" />
              <span>记住密码</span>
            </div>
            <input id="button" type="button" value="登&nbsp;录" />
          </form>
        </div>
      </section>
    </div>
  </body>
  <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
  <script>
    $("#button").click(function () {
      var name = $("#nameinput").val();
      var password = $("#passwordinput").val();
      var namerep = /^[a-zA-Z0-9_-]{4,16}$/;
      var passwordrep = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{8,10}$/;
      if (
        name === "" ||
        name === null ||
        password === "" ||
        password === null
      ) {
        layer.msg("用户名或密码不能为空，请重新输入", {
          time: 2000, //20s后自动关闭
          btn: ["知道了"],
        });
        return;
      } else {
        // if (namerep.test(name) || passwordrep.test(password)) {
          $.ajax({
            url: "http://127.0.0.1:8080/api/v1/system/login",
            type: "POST",
            data: JSON.stringify({
              username: name,
              password: password,
            }),
            dataType: "json",
            success: function (res) {
              console.log(res);
              if (res.Code == 0) {
                localStorage.setItem("token", res.Data.token);
                // window.open("../first.html");
                window.location.href="../first.html"
              } else {
                alert(res.Message);
              }
            },
            error: function () {
              alert("请求失败！");
            },
          });
        // } else {
        //   layer.msg("用户名或密码错误，请重新输入", {
        //     time: 2000, //20s后自动关闭
        //     btn: ["知道了"],
        //   });
        //   return;
        // }
      }
    });
  </script>
</html>
